package com.licq.ldoc.writer;

import java.io.FileNotFoundException;
import java.util.List;

import com.licq.ldoc.domain.Element;
import com.lowagie.text.DocWriter;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

public abstract class Writer {
	protected static ThreadLocal<DocFormat> currentFormat = new ThreadLocal<DocFormat>();
	
	public static DocFormat getCurrentFormat() {
		return currentFormat.get();
	}
	
	@SuppressWarnings("static-access")
	protected void setCurrentFormat(DocFormat format) {
		this.currentFormat.set(format);
	}
	
	public static boolean isInternalAnchorEnabled() {
		return getCurrentFormat().isInternalAchorEnabled();
	}
	
    public abstract void write(List<Element> elements) throws FileNotFoundException, DocumentException;
	
	public static enum DocFormat {
		Pdf(PdfWriter.class, true), 
		Rtf(RtfWriter2.class, false), 
		Htm(HtmlWriter.class, true),
		Html(HtmlWriter.class, true);
		
		private Class<? extends DocWriter> writerClazz;
		
		private boolean internalAchorEnabled;
		
		private DocFormat(Class<? extends DocWriter> writerClazz,
				boolean internalAchorEnabled) {
			this.writerClazz = writerClazz;
			this.internalAchorEnabled = internalAchorEnabled;
		}

		public Class<? extends DocWriter> getWriterClazz() {
			return writerClazz;
		}

		public boolean isInternalAchorEnabled() {
			return internalAchorEnabled;
		}

		public static DocFormat from(String extension) {
			for (DocFormat format : values()) {
				if (format.name().equalsIgnoreCase(extension)) {
					return format;
				}
			}
			return Rtf;
		}
	}
    
}
